package com.ItemsRent.action;

import java.util.HashMap;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ItemsRent.IService.IOrderForm;
import com.ItemsRent.model.Customer;
import com.ItemsRent.model.ItemTable;
import com.ItemsRent.model.OrderForm;

public class OrderFormAction {
	private Integer orderId;
	private Integer itemId;
	private Integer orderPrice;
	private Integer count;// 订单中该物品的数量
	private String evaluate;
	private Integer orderStatus;

	private Customer customer;
	private ItemTable item;
	private OrderForm orderForm;
	private IOrderForm orderFormService;
	private HashMap<String, Object> result = new HashMap<String, Object>();

	public HashMap<String, Object> getResult() {
		return result;
	}

	public void setResult(HashMap<String, Object> result) {
		this.result = result;
	}

	public Integer getOrderId() {
		return orderId;
	}

	public void setOrderId(Integer orderId) {
		this.orderId = orderId;
	}

	public Integer getItemId() {
		return itemId;
	}

	public void setItemId(Integer itemId) {
		this.itemId = itemId;
	}

	public Integer getOrderPrice() {
		return orderPrice;
	}

	public void setOrderPrice(Integer orderPrice) {
		this.orderPrice = orderPrice;
	}

	public Integer getCount() {
		return count;
	}

	public void setCount(Integer count) {
		this.count = count;
	}

	public String getEvaluate() {
		return evaluate;
	}

	public void setEvaluate(String evaluate) {
		this.evaluate = evaluate;
	}

	public Integer getOrderStatus() {
		return orderStatus;
	}

	public void setOrderStatus(Integer orderStatus) {
		this.orderStatus = orderStatus;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public ItemTable getItem() {
		return item;
	}

	public void setItem(ItemTable item) {
		this.item = item;
	}

	public OrderForm getOrderForm() {
		return orderForm;
	}

	public void setOrderForm(OrderForm orderForm) {
		this.orderForm = orderForm;
	}

	public IOrderForm getOrderFormService() {
		return orderFormService;
	}

	public void setOrderFormService(IOrderForm orderFormService) {
		this.orderFormService = orderFormService;
	}

	// 添加订单
	public void saveOrder() {
		customer = (Customer) ServletActionContext.getRequest().getSession()
				.getAttribute("customer");
		//System.out.println("下单中" + itemId);
		ApplicationContext ac = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		ItemTableAction itemTableAction = (ItemTableAction) ac
				.getBean("itemTableAction");
		// ItemTableAction
		// itemTableAction=(ItemTableAction)htemplate.get(ItemTableAction.class,
		// "1");
		item = itemTableAction.FindItemTableById(itemId);
		// BaseDAO<ItemTable> dao = new BaseDAO<ItemTable>();
		// item=dao.find(ItemTable.class, itemId);
		// orderFormService.findByOrderId(1);
		//System.out.println(item.getItemName());
		orderPrice = item.getItemPrice() * count;
		orderForm = new OrderForm(customer, item, orderPrice);
		orderFormService.insert(orderForm);
		//System.out.println("您已成功下单");
	}

	// 删除订单
	public void deleteOrder() {
		orderForm = orderFormService.findByOrderId(orderId);
		orderFormService.delete(orderForm);
		System.out.println("成功删除订单"+orderId);
	}

	// 通过订单ID找订单对象
	public String findOrderByOrderId() {
		orderForm = orderFormService.findByOrderId(orderId);
		// System.out.println("OrderForm: " + orderForm);
		result.put("order", orderForm);
		return "success";
	}

	// 通过订单ID找订单,根据功能，只找那些商家未确认的订单
	public String findOrderByCustomerId() {
		customer = (Customer) ServletActionContext.getRequest().getSession()
				.getAttribute("customer");
		List<OrderForm> list = orderFormService.findByCustomerId(customer.getCustomerId());
		/*
		 System.out.println("Book OrderForm: "); for (OrderForm order : list)
			  { System.out.println(order.toString()); }
			  */
		result.put("order", list);
		return "success";
	}

	public String showAllBook() {// 显示所有该用户 已下的 并且商家已确认的 订单，
		customer = (Customer) ServletActionContext.getRequest().getSession()
				.getAttribute("customer");
		List<OrderForm> list = orderFormService.findAllBook(customer
				.getCustomerId());
		/*
		 * System.out.println("Book OrderForm: "); for (OrderForm order : list)
		 * { System.out.println(order.toString()); }
		 */
		// OrderForm o=new OrderForm();
		// o.setOrderId(7);
		// o.setOrderPrice(50);
		result.put("book", list);
		// JSONObject json = JSONObject.fromObject(result);
		// System.out.println(json.toString());
		return "success";
	}

	public String showAllReceive() {// 显示该用户收到的所有订单
		customer = (Customer) ServletActionContext.getRequest().getSession()
				.getAttribute("customer");
		List<OrderForm> list = orderFormService.findAllReceive(customer
				.getCustomerId());
		/*
		 * System.out.println("Receive OrderForm: "); for (OrderForm order :
		 * list) { System.out.println(order.toString()); }
		 */
		result.put("receive", list);
		// JSONObject json = JSONObject.fromObject(result);
		// String result = json.toString();
		// System.out.println(json.toString());
		return "success";
	}
	
	
	// 更新修改订单的状态
	public void updateOrderStatus() {
		//System.out.println(orderStatus);
		orderForm = orderFormService.findByOrderId(orderId);
		if(orderStatus==1){
			ApplicationContext ac = new ClassPathXmlApplicationContext(
					"applicationContext.xml");
			ItemTableAction itemTableAction = (ItemTableAction) ac
					.getBean("itemTableAction");
			ItemTable it=orderForm.getItem();
			int n=it.getItemNumber()-1;
			if(n>=0)it.setItemNumber(n);
			else orderStatus=2;
			itemTableAction.updateItemTable(it);
		}
		orderForm.setOrderStatus(orderStatus);
		orderFormService.update(orderForm);
	}
	
	//给订单添加评价
	public void evaluate(){
		orderForm = orderFormService.findByOrderId(orderId);
		orderForm.setEvaluate(evaluate);
		orderFormService.update(orderForm);
	}


	
	/*
	 * //本想用于测试的 public static void main(String args[]){ OrderFormAction
	 * orderFormAction = new OrderFormAction(); orderFormAction.orderFormService
	 * = new OrderFormImpl(); orderFormAction.showAllBook(); }
	 */
}
